package com.ljx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.R;
import com.ljx.client.UcenterInterface;
import com.ljx.pojo.StatisticsDaily;
import com.ljx.mapper.StatisticsDailyMapper;
import com.ljx.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 李加喜
 * @since 2020-10-02
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterInterface ucenterInterface;
    @Override
    public void registerCount(String day) {
        //在添加数据之前先删除表里面相同日期的数据
        //防止产生相同时间不同值的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        R register = ucenterInterface.countRegister(day);
        Integer count = (Integer) register.getData().get("count");
        //将获取的数据添加到数据库文件里面
        //这里主要是通统计注册的人数  所以其他的数据记性随机生成
        //把获取数据添加数据库，统计分析表里面
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(count); //注册人数
        sta.setDateCalculated(day);//统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(sta);
    }
    //显示
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //输入的type对应的值正好与数据库中的字段名一样
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);//只查询date_calculated和type这一列
        //只查询这两列数据  type在前端设置的名字与数据库名字一样
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);
        //但是
        System.out.println(list+"=======");
        //System.out.println(list);
        //因为需要查询两列数据
        //一列是日期 一列是不同的日期对应的数量
        //前端要求数组的json结构["a","a","a"]
        //集合在前端的显示就是一种数组的形式
        List<String> dataList = new ArrayList<>(); //时间
        List<Integer> numDataList = new ArrayList<>(); //各个时间对应的数据

        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < list.size(); i++) {
            StatisticsDaily daily = list.get(i);
            //封装日期list集合
            dataList.add(daily.getDateCalculated());
            //封装对应数量  是哪个type才能获取那个属性的值
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("dataList",dataList);
        map.put("numDataList",numDataList);
        return map;
    }
    //实现饼状图显示
    @Override
    public Map<String, Object> getshowDataBing(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);//查询date_calculated和type(前后名字一样)这两列
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);
        ArrayList<String> dataList = new ArrayList<>();
        ArrayList<Map<String, Object>> listData = new ArrayList<>();

        for (int i=0;i<list.size();i++){
            HashMap<String, Object> map = new HashMap<>();
            StatisticsDaily daily = list.get(i);
            dataList.add(daily.getDateCalculated());//保存添加的时间  就是饼状图要显示的名称
            map.put("name",daily.getDateCalculated());//添加时间
            System.out.println(daily.getDateCalculated()+"DDDD");
            switch (type){
                case "login_num":
                    map.put("value",daily.getLoginNum());
                    break;
                case "register_num":
                    map.put("value",daily.getRegisterNum());
                    break;
                case "video_view_num":
                    map.put("value",daily.getVideoViewNum());
                    break;
                case "course_num":
                    map.put("value",daily.getCourseNum());
                    break;
                default:
                    break;
            }
            listData.add(map);
            System.out.println(listData);
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("list",listData);
        hashMap.put("name",dataList);
        return hashMap;
    }

}
